# Adogs
# 时间: 2021/7/17 11:24
import pygame
import random

from actor import WalkDir
from actor.walk_soldier import WalkSoldier


class JapaneseWalkSoldier(WalkSoldier):
    """
    日本人行走战士类
    方法；
    1.__init__(x, y)
    2.run(down_flag, key_list, obstacle_group)
    """
    def __init__(self, x, y):
        """
        初始化函数
        :param x:人物的x坐标
        :param y: 人物的y坐标
        """
        soldier_id = random.randint(1, 4)   # 随机从三类日本人物中选取一个
        walk_imgpath = "./resources/picture/japanese/soldier{0}.png".format(soldier_id)
        super(JapaneseWalkSoldier, self).__init__(walk_imgpath, x, y, "小日本" + str(soldier_id), 100)
        self.count = -5  # 计数器，用于控制人物的自动行走和停止
        self.key = pygame.K_s   # 初始化方向向下
        self.rect = pygame.Rect(self.pos_x + 15, self.pos_y + 85, 20, 10)

    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group):
        """
        人物运动函数
        :param down_flag: bool类型，判断是否是点击按键
        :param key_list: 列表类型，存储键盘上每一个按键的bool值，判断按键是否被按下
        :param obstacle_group: 列表型，存储障碍物组
        :return: 无返回值
        """
        if self.count == -5:    # 当计数器等于-5时就重新生成行走方向
            key = random.randint(0, 3)  # 随机生成行走方向
            self.run_flag = True
            self.count = 15
            if key == 0:
                self.walk_action.set_dir(WalkDir.down)
            elif key == 1:
                self.walk_action.set_dir(WalkDir.left)
            elif key == 2:
                self.walk_action.set_dir(WalkDir.right)
            elif key == 3:
                self.walk_action.set_dir(WalkDir.on)

        elif self.count >= 4:   # 当计数器大于4时就一直行走，并判断是否碰到障碍物
            self.count -= 1
            off_x = 0
            off_y = 0
            if self.walk_action.walk_dir == WalkDir.down:
                off_y += 10
            elif self.walk_action.walk_dir == WalkDir.on:
                off_y += -10
            elif self.walk_action.walk_dir == WalkDir.left:
                off_x += -10
            elif self.walk_action.walk_dir == WalkDir.right:
                off_x += 10
            temp_rect = self.rect
            self.rect = self.rect.move(off_x, off_y)
            collide_list = pygame.sprite.spritecollide(self, obstacle_group, False)
            if len(collide_list) > 0:
                self.rect = temp_rect
            else:
                self.pos_x += off_x
                self.pos_y += off_y

        else:
            self.run_flag = False
            self.count -= 1
